r语言中对向量求条件语句
To advance with programming, we need to be able to control the flow of the program execution. This controlling happens based upon certain logical conditions. When a condition is met, we expect the program to behave in a certain way, and when a condition is not met, we steer the program in a different way. This is established by means of if else
statements in R like several other programming languages.
为了进行编程,我们需要能够控制程序执行的流程。 该控制基于某些逻辑条件进行。 当满足条件时,我们希望程序以某种方式运行,而当不满足条件时,我们将以另一种方式操纵程序。 与if else
几种编程语言一样,这是通过R中的if else
语句建立的。
R中的If语句 (If Statement in R)
R supports several variants of conditional statements like stand-alone if, if with else, nested if and switch. We will explore one by one in this tutorial. The basic structure of an if block is as follows.
R支持条件语句的多种变体,例如独立if(如果带有else),嵌套if和switch。 在本教程中,我们将一一探讨。 if块的基本结构如下。
if (condition) {
#Code to execute if condition is met
} #This code only works if the condition is true.
Let us illustrate this with a simple example.
让我们用一个简单的例子来说明这一点。
if (a<=5) {
print(a^2)
}
The above code simply checks if a value is less than or equal to 5, and if the condition holds good, it prints the squared value